package de.bazinga.rotw.network.message.physics;

import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.message.type.CertifiedMessage;
import com.captiveimagination.jgn.message.type.PlayerMessage;
import com.captiveimagination.jgn.message.type.TimestampedMessage;
import com.jme.math.Vector3f;
import com.jme.scene.Spatial;
import com.jmex.jbullet.nodes.PhysicsNode;

import de.bazinga.rotw.collision.CollidableObjects;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.network.Server;
import de.bazinga.rotw.network.message.GameMessage;

public class ObjectLocationMessage extends Message implements CertifiedMessage, PlayerMessage, TimestampedMessage, GameMessage {
	private Vector3f newLocation = null;
	private int physicsIndex = 0;
	
	
	
	public void handleMessage(RotwGameState game, Message msg, boolean server) {
		if(!(msg instanceof ObjectLocationMessage)) return ;
		
		if(CollidableObjects.getInstance().getObjects("physics").size()  > getPhysicsIndex() && getPhysicsIndex() >= 0) {
			// apply force
			Spatial obj = CollidableObjects.getInstance().getObjects("physics").get(getPhysicsIndex());
			if(obj instanceof PhysicsNode) {
				obj.setLocalTranslation(
						newLocation.x, 
						newLocation.y, 
						newLocation.z);
			}
		}
		
		if(server) {	
			((Server)game.getSyncManager()).getServer().sendToAllExcept(this, getPlayerId());
		}
	}



	public void setPhysicsIndex(int physicsIndex) {
		this.physicsIndex = physicsIndex;
	}



	public int getPhysicsIndex() {
		return physicsIndex;
	}
	
	
	public Vector3f getNewLocation() {
		return newLocation;
	}



	public void setNewLocation(Vector3f newLocation) {
		this.newLocation = newLocation;
	}
}